package JET.UtilPanels;

import JET.Centers.ControlCenter;
import JET.Centers.Graphics.GUICenter;
import JET.Scene;
import JET.StaticActions;
import JET.mygui.UtilTabsPane.UtilTabsPaneTab;
import java.awt.event.KeyEvent;

/*
 * UPRSpring.java
 *
 * Created on 2010-06-15, 00:03:09
 */

/**
 *
 * @author 057Ry
 */
public class UPRSpring extends javax.swing.JPanel implements UtilTabsPaneTab {
    
    static final ControlCenter CC = ControlCenter.getInstance();
    private Scene scene;

    /** Creates new form UPRSpring */
    public UPRSpring() {
        initComponents();
    }

    public void setScene(Scene s) {
        scene = s;
    }

    public GUICenter.UtilTabs getId() {
        return GUICenter.UtilTabs.RSpring;
    }

    public boolean isScrollable() {
        return true;
    }

    private void setRSpringsKs()
    {
        double ks =0.;

        try {
            ks = getRSpringsKsGUIValue();
        }catch(NumberFormatException nfe) {
            System.out.println("Cannot parse this value into double number format ...");
            return;
        }

        StaticActions.setSelectedRSpringsKs(ks, scene);
    }

    private void setRSpringsKd()
    {
        double kd = 0.;

        try {
            kd = getRSpringsKdGUIValue();
        }catch(NumberFormatException nfe){
            System.out.println("Cannot parse this value into double number format ...");
            return;
        }

        StaticActions.setSelectedRSpringsKd(kd, scene);
    }

    private void setStartAngle()
    {
        double startAngle=0.;

        try {
            startAngle = getStartAngleGUIValue();
        }catch(NumberFormatException nfe) {
            System.out.println("Cannot parse this value into double number format ...");
            return;
        }

        StaticActions.setSelectedRSpringsStartAngle(startAngle, scene);
    }

    private void setStartAngleAsCurrent()
    {
        StaticActions.setSelectedRSpringsStartAngleAsCurrent(scene);
    }

    private void setRSpringsAll() {
        setRSpringsKs();
        setRSpringsKd();
        setStartAngle();
    }

    public double getRSpringsKsGUIValue()
    {
        return Double.parseDouble( rSpringsKsTextField.getText() );
    }

    public void setRSpringsKsGUIValue(double ks)
    {
        rSpringsKsSlider.setValue( (int)(ks*10) );
        rSpringsKsTextField.setText(""+(ks));
    }

    public double getRSpringsKdGUIValue()
    {
        return Double.parseDouble( rSpringsKdTextField.getText() );
    }

    public void setRSpringsKdGUIValue(double kd)
    {
        rSpringsKdSlider.setValue( (int)(kd*10) );
        rSpringsKdTextField.setText( ""+(kd));
    }

    public void setStartAngleGUIValue(double value)
    {
        startAngleSlider.setValue( (int)(value) );
        startAngleTextField.setText(""+value);
    }

    public double getStartAngleGUIValue()
    {
        return Math.toRadians( Double.parseDouble( startAngleTextField.getText() ));
    }

    public void setRSpringsKsText(String text)
    {
        rSpringsKsTextField.setText(text);
    }

    public void setRSpringsKdText(String text)
    {
        rSpringsKdTextField.setText(text);
    }

    public void setStartAngleText(String text)
    {
        startAngleTextField.setText(text);
    }

    public void bringFocusToRSpringsKsTextField()
    {
        rSpringsKsTextField.requestFocus();
        rSpringsKsTextField.select(0, rSpringsKsTextField.getText().length());

    }

    public void bringFocusToRSpringsKdTextField()
    {
        rSpringsKdTextField.requestFocus();
        rSpringsKdTextField.select(0, rSpringsKdTextField.getText().length());
    }

    public void bringFocusToStartAngleTextField()
    {
        startAngleTextField.requestFocus();
        startAngleTextField.select(0, startAngleTextField.getText().length());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        rSpringsKsTextField = new javax.swing.JTextField();
        rSpringsKdTextField = new javax.swing.JTextField();
        rSpringsKsSlider = new javax.swing.JSlider();
        rSpringsKdSlider = new javax.swing.JSlider();
        jLabel9 = new javax.swing.JLabel();
        startAngleTextField = new javax.swing.JTextField();
        startAngleSlider = new javax.swing.JSlider();
        startLengthButton3 = new javax.swing.JButton();

        jLabel7.setText("Ks");

        jLabel8.setText("Kd");

        rSpringsKsTextField.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        rSpringsKsTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                rSpringsKsTextFieldKeyPressed(evt);
            }
        });

        rSpringsKdTextField.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        rSpringsKdTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                rSpringsKdTextFieldKeyPressed(evt);
            }
        });

        rSpringsKsSlider.setMaximum(500);
        rSpringsKsSlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                rSpringsKsSliderStateChanged(evt);
            }
        });
        rSpringsKsSlider.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                rSpringsKsSliderMouseDragged(evt);
            }
        });

        rSpringsKdSlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                rSpringsKdSliderStateChanged(evt);
            }
        });
        rSpringsKdSlider.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                rSpringsKdSliderMouseDragged(evt);
            }
        });

        jLabel9.setText("Start Angle");

        startAngleTextField.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        startAngleTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                startAngleTextFieldKeyPressed(evt);
            }
        });

        startAngleSlider.setMaximum(360);
        startAngleSlider.setValue(0);
        startAngleSlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                startAngleSliderStateChanged(evt);
            }
        });
        startAngleSlider.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                startAngleSliderMouseDragged(evt);
            }
        });

        startLengthButton3.setText("Recalculate Start Angles");
        startLengthButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startLengthButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rSpringsKsSlider, 0, 0, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addGap(18, 18, 18)
                        .addComponent(rSpringsKsTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE))
                    .addComponent(rSpringsKdSlider, 0, 0, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addGap(10, 10, 10)
                        .addComponent(rSpringsKdTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE))
                    .addComponent(jLabel9)
                    .addComponent(startAngleSlider, 0, 0, Short.MAX_VALUE)
                    .addComponent(startAngleTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)
                    .addComponent(startLengthButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 113, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(rSpringsKsTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(1, 1, 1)
                .addComponent(rSpringsKsSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(rSpringsKdTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rSpringsKdSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(startAngleTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(startAngleSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(startLengthButton3)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getAccessibleContext().setAccessibleName("Rotary spring properties");
    }// </editor-fold>//GEN-END:initComponents

    private void rSpringsKsSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_rSpringsKsSliderStateChanged
        rSpringsKsTextField.setText( ""+rSpringsKsSlider.getValue()*0.1 );
}//GEN-LAST:event_rSpringsKsSliderStateChanged

    private void rSpringsKdSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_rSpringsKdSliderStateChanged
        rSpringsKdTextField.setText( ""+rSpringsKdSlider.getValue()*0.1 );
}//GEN-LAST:event_rSpringsKdSliderStateChanged

    private void startAngleSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_startAngleSliderStateChanged
        startAngleTextField.setText( ""+startAngleSlider.getValue() );
}//GEN-LAST:event_startAngleSliderStateChanged

    private void startLengthButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startLengthButton3ActionPerformed
        setStartAngleAsCurrent();
}//GEN-LAST:event_startLengthButton3ActionPerformed

    private void rSpringsKsTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_rSpringsKsTextFieldKeyPressed
        switch (evt.getKeyCode()) {
            case KeyEvent.VK_ENTER :
                setRSpringsKs();
                break;
        }
    }//GEN-LAST:event_rSpringsKsTextFieldKeyPressed

    private void rSpringsKdTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_rSpringsKdTextFieldKeyPressed
        switch (evt.getKeyCode()) {
            case KeyEvent.VK_ENTER :
                setRSpringsKd();
                break;
        }
    }//GEN-LAST:event_rSpringsKdTextFieldKeyPressed

    private void startAngleTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_startAngleTextFieldKeyPressed
        switch (evt.getKeyCode()) {
            case KeyEvent.VK_ENTER :
                setStartAngle();
                break;
        }
    }//GEN-LAST:event_startAngleTextFieldKeyPressed

    private void rSpringsKsSliderMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_rSpringsKsSliderMouseDragged
        setRSpringsKs();
    }//GEN-LAST:event_rSpringsKsSliderMouseDragged

    private void rSpringsKdSliderMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_rSpringsKdSliderMouseDragged
        setRSpringsKd();
    }//GEN-LAST:event_rSpringsKdSliderMouseDragged

    private void startAngleSliderMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_startAngleSliderMouseDragged
        setStartAngle();
    }//GEN-LAST:event_startAngleSliderMouseDragged


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JSlider rSpringsKdSlider;
    private javax.swing.JTextField rSpringsKdTextField;
    private javax.swing.JSlider rSpringsKsSlider;
    private javax.swing.JTextField rSpringsKsTextField;
    private javax.swing.JSlider startAngleSlider;
    private javax.swing.JTextField startAngleTextField;
    private javax.swing.JButton startLengthButton3;
    // End of variables declaration//GEN-END:variables

}
